package com.bos.prop.controller.json;

import com.bos.dao.BankDAO;
import com.bos.dao.KelurahanDAO;
import com.bos.dao.SekolahDAO;
import com.bos.domain.Bank;
import com.bos.domain.Kelurahan;
import com.bos.domain.Sekolah;
import com.bos.editor.BankEditor;
import com.bos.editor.KelurahanEditor;
import com.bos.editor.SekolahEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class SekolahJsonController {

    private SekolahDAO sekolahDAO;
    private BankDAO bankDAO;
    private KelurahanDAO kelurahanDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setSekolahDAO(SekolahDAO sekolahDAO) {
        this.sekolahDAO = sekolahDAO;
    }

    @Autowired
    public void setBankDAO(BankDAO bankDAO) {
        this.bankDAO = bankDAO;
    }

    @Autowired
    public void setKelurahanDAO(KelurahanDAO kelurahanDAO) {
        this.kelurahanDAO = kelurahanDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/sekolah.json", method = RequestMethod.GET)
    public
    @ResponseBody
    List<Sekolah> index() {
        try {
            return sekolahDAO.getAll();
        } catch (DataAccessException e) {
            return new ArrayList<Sekolah>();
        }
    }

    @RequestMapping(value = "/sekolah/{npsn}.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Sekolah show(@PathVariable("npsn") Sekolah sekolah) {
        return sekolah;
    }

    @RequestMapping(value = "/sekolah.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody Sekolah sekolah) {
        if (sekolah == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            sekolahDAO.save(sekolah);
            result.put("success", true);
            result.put("sekolah", sekolah);
            result.put("message", "Sukses Save Sekolah " + sekolah);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("sekolah", sekolah);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Sekolah " + sekolah);
        }
        return result;
    }

    @RequestMapping(value = "/sekolah/{npsn}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("npsn") Sekolah sekolah) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            sekolahDAO.delete(sekolah);
            result.put("success", true);
            result.put("message", "Sukses Delete Sekolah " + sekolah);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("message", "Gagal Delete Sekolah " + sekolah);
        }
        return result;
    }

    @RequestMapping(value = "/sekolah.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody Sekolah sekolah) {
        if (sekolah == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            sekolahDAO.merge(sekolah);
            result.put("success", true);
            result.put("sekolah", sekolah);
            result.put("message", "Sukses Update Sekolah " + sekolah);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("sekolah", sekolah);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Update Sekolah " + sekolah);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Bank.class, new BankEditor(bankDAO));
        binder.registerCustomEditor(Kelurahan.class, new KelurahanEditor(kelurahanDAO));
        binder.registerCustomEditor(Sekolah.class, new SekolahEditor(sekolahDAO));
    }

}
